import loading from '@/components/xuan-loading/xuan-loading.vue'
import cLabel from "@/components/cat-label/cat-label.vue"
import fromItem from '@/components/from-item/from-item.vue'
import wx from "weixin-js-sdk";
import lsjUpload from '@/uni_modules/lsj-upload/components/lsj-upload/lsj-upload.vue';
import {
	log
} from 'util'
export default {
	props: {
		mustIcon: {
			type: String,
			default: require('@/static/Scope.png'),
		},
		unIcon: {
			type: String,
			default: require('@/static/unscope.png'),
		}
	},
	data() {
		return {
			option: {},
			heights: '156rpx',
			size: 10,
			debug: true,
			// 选择文件后是否立即自动上传
			instantly: true,
			// 微信小程序Map对象for循环不显示，所以转成普通数组，不要问为什么，我也不知道
			count: 8,

			Inv: 1,
			bihe: false, //闭合是否禁用
			status: null,
			yiji: false,
			erji: false,
			statusNum: null, //调用那个保存接口

			positionData: [], //存放位置
			page: false,
			click: '',
			appliance: null,
			newDisabled: false, //基本信息是否可编辑
			newMust: "must", //基本信息必填
			flagNum: -1,
			// check:false,//处理是否可编辑
			gridmanList: [], //所有网格员
			selector: "text",
			date1: "date1",
			// 弹框
			selectParent: true,
			multiple: false,
			companyTree: [], //树结构	
			TreeNum: null, //第几个树结构
			flod: true,

			// loading样式
			width: '150rpx',
			height: '100rpx',
			shadeShow: false,
			backgroundColor: "transparent",
			themeColor: '#33CCCC',
			// loading样式
			closeFormObj: {
				fId: "", //必传
				userId: "", //当前登录人id
				userName: "", //当前登录人姓名
				recordfiles: [], //删除文件id集合
				fNonresult: "", //不合格参数及结果
				fProcessing: "", //处理方式
				fProcessusername: "", //处理人姓名
				fProcessuserid: "", //处理人id
				fProcessdates: "", //处理日期
				fProcessfileurl: "" //闭合申请单附件
			},
			secondList: [],
			FirstList: [],
			FirstLevel: {
				fId: "", //必传
				userId: "", //当前登录人id
				userName: "", //当前登录人姓名
				recordfiles: [], //删除文件id集合
				fCheckresult1: "", //一级审核结果
				fCheckopinion1: "", //一级审核意见
				fCheckuserid1: "", //一级审核人id
				fCheckusername1: "", //一级审核人姓名
				fCheckdate1s: "", //一级审核日期
				fCheckfileurl1: "" //一级审核附件
			},
			fileList: [],
			secondLevel: {
				fId: "", //必传
				userId: "", //当前登录人id
				userName: "", //当前登录人姓名
				recordfiles: [], //删除文件id集合
				fCheckresult2: "", //一级审核结果
				fCheckopinion2: "", //一级审核意见
				fCheckuserid2: "", //一级审核人id
				fCheckusername2: "", //一级审核人姓名
				fCheckdate2s: "", //一级审核日期
				fCheckfileurl2: "" //一级审核附件
			},

			ifArr: ['同意', '不同意'],
			totalData: {}, // 详情
			flagNum: -1,
			flagNum2: -1,

			userInfo: null,
			must: "must",
			disabled: false,
			recordfiles: [], //删除文件集合
			optionsF: null, //上个页面传递过来的参数
			timing: null, //当前年月日
			//显示隐藏
			basic: 1, //基本信息
			close: 1, //问题处理情况
			timing: null,


		};
	},
	onLoad(options) {
		console.log(options)
		var that = this;
		this.status = options.status
		if (this.status == '待处理' || this.status == '一级审核未通过') {
			this.bihe = false
			this.yiji = true
			this.erji = true
			this.statusNum = "1"
			this.Inv = 1
		} else if (this.status == '一级待审核' || this.status == '二级审核未通过') {
			this.bihe = true
			this.yiji = false
			this.erji = true
			this.statusNum = "2"
			this.Inv = 2
		} else if (this.status == '二级待审核') {
			this.bihe = true
			this.yiji = true
			this.erji = false
			this.statusNum = "3"
			this.Inv = 3
		} else if (this.status == '已完成') {
			this.bihe = true
			this.yiji = true
			this.erji = true
			this.statusNum = "4"
			this.Inv = 0
		}
		uni.getStorage({ //监督检查方式
			key: 'userInfo',
			success: function(res) {
				that.userInfo = JSON.parse(res.data);

				that.closeFormObj.fProcessusername = that.userInfo.realName //入库人员
				that.closeFormObj.fProcessuserid = that.userInfo.userId //入库人员id
				that.closeFormObj.userName = that.userInfo.realName //入库人员
				that.closeFormObj.userId = that.userInfo.userId //入库人员id
				// 一级审核
				that.FirstLevel.fCheckusername1 = that.userInfo.realName //入库人员
				that.FirstLevel.fCheckuserid1 = that.userInfo.userId //入库人员id
				that.FirstLevel.userName = that.userInfo.realName //入库人员
				that.FirstLevel.userId = that.userInfo.userId //入库人员id
				that.FirstLevel.fId = options.id

				that.secondLevel.fCheckusername2 = that.userInfo.realName //入库人员
				that.secondLevel.fCheckuserid2 = that.userInfo.userId //入库人员id
				that.secondLevel.userName = that.userInfo.realName //入库人员
				that.secondLevel.userId = that.userInfo.userId //入库人员id
				that.secondLevel.fId = options.id



				that.optionsF = options;
				that.closeFormObj.fId = options.id
				that.getDetailData(options.id)

			}
		});
		// || MP-WEIXIN || H5
		// #ifdef APP-PLUS
		this.playVideo()
		uni.$on('drawer-page', (data) => {
			that.clickNum(data)
		})
		// #endif
		// #ifdef H5
		this.appliance = 1
		// that.geterweimaInfo()
		// #endif
	},
	onShow() {

	},
	components: {
		loading,
		cLabel,
		fromItem,
	},

	onUnload() {
		uni.$off('drawer-page')
	},
	onReady() {

		this.$nextTick(() => {
			this.$refs.lsjUpload.show();
		})
		var that = this
		if (this.statusNum == 1) {


			if (that.totalData.fProcessfileurl == "" && that.totalData.fProcessfileurl == null && that.totalData
				.fProcessfileurl ==
				undefined) {
				console.log("文件么准备好")
			} else {
				setTimeout(() => {

					uni.getStorage({
						key: 'userInfo',
						success: function(res) {
							that.userInfo = JSON.parse(res.data)
							var folderid = that.totalData.fProcessfileurl
							var userId = that.userInfo.userId
							var userName = that.userInfo.realName
							console.log(userId)
							var url = that.$http.upload + '?folderid=' + folderid + '&userId=' +
								userId + '&userName=' +
								userName;
							that.option = {
								// 上传服务器地址，此地址需要替换为你的接口地址
								url: url,
								// 上传附件的key
								name: 'file',
								header: {},
								formData: {}
							};
						}
					})
				}, 3000)
			}
		} else if (this.statusNum == 2) {

			// FirstLevel.fCheckfileurl1
			if (that.totalData.fCheckfileurl1 == "" && that.totalData.fCheckfileurl1 == null && that.totalData
				.fCheckfileurl1 ==
				undefined) {
				console.log("文件么准备好")
			} else {
				setTimeout(() => {

					uni.getStorage({
						key: 'userInfo',
						success: function(res) {
							that.userInfo = JSON.parse(res.data)
							var folderid = that.totalData.fCheckfileurl1
							var userId = that.userInfo.userId
							var userName = that.userInfo.realName
							console.log(userId)
							var url = that.$http.upload + '?folderid=' + folderid + '&userId=' +
								userId + '&userName=' +
								userName;
							that.option = {
								// 上传服务器地址，此地址需要替换为你的接口地址
								url: url,
								// 上传附件的key
								name: 'file',
								header: {},
								formData: {}
							};
						}
					})
				}, 3000)
			}
		} else if (this.statusNum == 3) {

			// FirstLevel.fCheckfileurl1
			if (that.totalData.fCheckfileurl2 == "" && that.totalData.fCheckfileurl2 == null && that.totalData
				.fCheckfileurl2 ==
				undefined) {
				console.log("文件么准备好")
			} else {
				setTimeout(() => {

					uni.getStorage({
						key: 'userInfo',
						success: function(res) {
							that.userInfo = JSON.parse(res.data)
							var folderid = that.totalData.fCheckfileurl2
							var userId = that.userInfo.userId
							var userName = that.userInfo.realName
							console.log(userId)
							var url = that.$http.upload + '?folderid=' + folderid + '&userId=' +
								userId + '&userName=' +
								userName;
							that.option = {
								// 上传服务器地址，此地址需要替换为你的接口地址
								url: url,
								// 上传附件的key
								name: 'file',
								header: {},
								formData: {}
							};
						}
					})
				}, 3000)
			}
		}
	},
	methods: {

		ViewImage(e, url) {
			var photo = []
			var file = null;
			if (this.statusNum == 1) {
				file = this.fileList
			} else if (this.statusNum == 2) {
				file = this.FirstList
			} else if (this.statusNum == 3) {
				file = this.secondList
			}

			file.map(item => {
				if (item.fFileType != '.pdf') {
					photo.push(item.url)
				}
			})
			console.log(e, url)
			uni.previewImage({
				urls: photo,
				current: url,
				loop: true,
				indicator: 'default'
			});
		},
		pdfPage(name, url) {
			var aa =
				url
			let links = encodeURIComponent(aa)

			uni.navigateTo({
				url: '/pages/apply/pdf?links=' + links + '&names=' + name,
			})

		},
		// 上传进度回调
		onprogress(item) {
			var that = this
			if (item.responseText) {
				var data = JSON.parse(item.responseText)
				console.log(444, data, data[0].atta.fFiletype)
				var url = data[0].atta.fFilepath
				var urlPath;
				console.log(99, that.statusNum, url)
				if (url.indexOf("D:") != -1) {
					urlPath = url.replace(that.$http.redf, that.$http.file)


					if (that.statusNum == 1) {
						that.fileList.push({
							url: urlPath,
							fFileType: data[0].atta.fFiletype,
							name: item.name,
							fId: data[0].atta.fId
						})
					} else if (that.statusNum == 2) {
						that.FirstList.push({
							url: urlPath,
							fFileType: data[0].atta.fFiletype,
							name: item.name,
							fId: data[0].atta.fId
						})
					} else if (that.statusNum == 3) {
						that.secondList.push({
							url: urlPath,
							fFileType: data[0].atta.fFiletype,
							name: item.name,
							fId: data[0].atta.fId
						})
					}
					console.log('闭合:', that.fileList, '一级:', that.FirstList, '二级:', that.secondList)
				}

				this.$refs.loading.close();
			}
		},
		// 文件选择回调
		onChange(files) {
			this.$refs.loading.open();

		},
		// 删除文件
		DelFile(e, id, index) {
			var that = this
			uni.showModal({
				title: '召唤师',
				content: '确定要删除这段回忆吗？',
				cancelText: '再看看',
				confirmText: '再见',
				success: res => {
					if (res.confirm) {





						if (this.statusNum == 1) {
							that.closeFormObj.recordfiles.push({
								fId: id
							})
							this.fileList.splice(index, 1)
						} else if (this.statusNum == 2) {

							that.FirstLevel.recordfiles.push({
								fId: id
							})
							this.FirstList.splice(index, 1)
						} else if (this.statusNum == 3) {
							that.secondLevel.recordfiles.push({
								fId: id
							})
							this.secondList.splice(index, 1)
						}


					}
				}
			})
		},

		yj_searchForm(num) {
			var that = this
			this.Inv = num;
			console.log(num)
		},


		clickNum(data) {
			this.saveData(this.statusNum);
		},


		getNewData() {
			var that = this;
		},
		// 详情
		getDetailData(id) {
			this.getDate();
			var that = this;
			that.fileList = [];
			var datas = {
				fId: id,
			}
			that.$http.getOutsourcednonByFid(datas).then(res => {
				if (res.errcode == 0) {
					that.totalData = res.data.yjQmpClOutsourcednon;
					that.closeFormObj.fNonresult = res.data.yjQmpClOutsourcednon.fNonresult
					// that.fileList = res.data.fProcessfileurllist
					// that.fileList = []
					that.closeFormObj.fProcessfileurl = res.data.yjQmpClOutsourcednon.fProcessfileurl

					that.FirstLevel.fCheckfileurl1 = res.data.yjQmpClOutsourcednon.fCheckfileurl1; //一级审核日期
					that.secondLevel.fCheckfileurl2 = res.data.yjQmpClOutsourcednon.fCheckfileurl2; //一级审核日期
					console.log('iii', that.totalData.fProcessdates)
					if (that.totalData.fProcessdates != null || that.totalData.fProcessdates != "") {
						this.closeFormObj = that.totalData
						this.closeFormObj.recordfiles = []
						that.fileList = res.data.fProcessfileurllist
						// this.FirstList
					} else {
						this.closeFormObj.fProcessdates = this.timing
						this.closeFormObj.fProcessusername = this.userInfo.realName; //处理人
						this.closeFormObj.fProcessuserid = this.userInfo.userId; //处理人id
					}


					if (that.totalData.fCheckdate1s != null || that.totalData.fCheckdate1s != "") {
						this.FirstLevel = that.totalData
						console.log(22222);
						this.FirstLevel.recordfiles = []
						this.FirstList = res.data.fCheckfileurl1list
						this.flagNum = 0
					} else {
						this.FirstLevel.fCheckdate1s = this.timing
						that.FirstLevel.fCheckusername1 = that.userInfo.realName //入库人员
						that.FirstLevel.fCheckuserid1 = that.userInfo.userId //入库人员id
					}
					if (that.totalData.fCheckdate2s != null || that.totalData.fCheckdate2s != "") {
						this.secondLevel = that.totalData
						console.log(22222);
						this.secondLevel.recordfiles = []
						this.secondList = res.data.fCheckfileurl1list
						this.flagNum2 = 0
					} else {
						this.secondLevel.fCheckdate2s = this.timing
						that.secondLevel.fCheckusername2 = that.userInfo.realName //入库人员
						that.secondLevel.fCheckuserid2 = that.userInfo.userId //入库人员id
					}

					that.page = true
					console.log(that.closeFormObj)

				} else {
					uni.showToast({
						title: "请求失败",
						icon: 'none',
						duration: 2000
					});
				}

			}, (err) => {
				uni.showToast({
					title: "接口报错",
					icon: 'none',
					duration: 2000
				});
			})

		},

		// 下拉框
		selectorFun(e) {
			console.log("下拉选择", e);

		},

		// 判断必填项
		saveData(data) {

			var that = this;
			// var num = 0
			if (data == 1) {

				if (this.closeFormObj.fProcessing == "" || this.closeFormObj.fProcessing == undefined) {
					uni.showToast({
						title: '处理方式不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else {


					that.$refs.loading.open();

					// console.log(num)
					//
					this.$http.saveOutsourcednon(this.closeFormObj).then(res => {
						if (res.errcode == 0) {
							// that.$refs.loading.close();
							that.page = false
							if (!that.click) {
								setTimeout(() => {
									uni.showToast({
										title: "保存成功",
										icon: 'none',
										duration: 2000
									});
									uni.redirectTo({
										url: "/pages/apply/outsourcedManage/outUnQualifiedReports/testing/index?Inv=" +
											1,
										success: () => {
											that.clickN = 1
											that.click = false
											that.$refs.loading.close();
											// #ifdef H5
											setTimeout(() => {
												that.click = false
											}, 100)
											// #endif
										}
									})
								}, 1500)
							}

							that.click = true

						} else {
							console.log("接口报错")
							that.$refs.loading.close();
							that.click = true
						}
					}, (err) => {
						that.$refs.loading.close();
						that.click = true
						uni.showToast({
							title: "请求失败",
							icon: 'none',
							duration: 2000
						});
					})
				}
			} else if (data == 3) {
				if (this.secondLevel.fCheckresult1 == "" || this.secondLevel.fCheckresult1 == undefined) {
					uni.showToast({
						title: '请选择审核结果',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (this.secondLevel.fCheckdate1s == "" || this.secondLevel.fCheckdate1s == undefined) {
					uni.showToast({
						title: '审核意见不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else {


					that.$refs.loading.open();

					// console.log(num)
					//
					this.$http.checkOutsourcednon2(this.closeFormObj).then(res => {
						if (res.errcode == 0) {
							// that.$refs.loading.close();
							that.page = false
							if (!that.click) {
								setTimeout(() => {
									uni.showToast({
										title: "保存成功",
										icon: 'none',
										duration: 2000
									});
									uni.redirectTo({
										url: "/pages/apply/outsourcedManage/outUnQualifiedReports/testing/index?Inv=" +
											1,
										success: () => {
											that.clickN = 1
											that.click = false
											that.$refs.loading.close();
											// #ifdef H5
											setTimeout(() => {
												that.click = false
											}, 100)
											// #endif
										}
									})
								}, 1500)
							}

							that.click = true

						} else {
							console.log("接口报错")
							that.$refs.loading.close();
							that.click = true
						}
					}, (err) => {
						that.$refs.loading.close();
						that.click = true
						uni.showToast({
							title: "请求失败",
							icon: 'none',
							duration: 2000
						});
					})
				}
			} else if (data == 2) {
				if (this.FirstLevel.fCheckresult1 == "" || this.FirstLevel.fCheckresult1 == undefined) {
					uni.showToast({
						title: '请选择审核结果',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else if (this.FirstLevel.fCheckdate1s == "" || this.FirstLevel.fCheckdate1s == undefined) {
					uni.showToast({
						title: '审核意见不能为空',
						icon: 'none',
						duration: 2000
					});
					return false;
				} else {


					that.$refs.loading.open();

					// console.log(num)
					//
					this.$http.checkOutsourcednon1(this.closeFormObj).then(res => {
						if (res.errcode == 0) {
							// that.$refs.loading.close();
							that.page = false
							if (!that.click) {
								setTimeout(() => {
									uni.showToast({
										title: "保存成功",
										icon: 'none',
										duration: 2000
									});
									uni.redirectTo({
										url: "/pages/apply/outsourcedManage/outUnQualifiedReports/testing/index?Inv=" +
											1,
										success: () => {
											that.clickN = 1
											that.click = false
											that.$refs.loading.close();
											// #ifdef H5
											setTimeout(() => {
												that.click = false
											}, 100)
											// #endif
										}
									})
								}, 1500)
							}

							that.click = true

						} else {
							console.log("接口报错")
							that.$refs.loading.close();
							that.click = true
						}
					}, (err) => {
						that.$refs.loading.close();
						that.click = true
						uni.showToast({
							title: "请求失败",
							icon: 'none',
							duration: 2000
						});
					})
				}
			}

		},


		// 原生窗体按钮显示
		playVideo() {
			let subNVue = uni.getSubNVueById('safe_notice')
			subNVue.show()
		},

		// 获取当前日期
		getDate() {
			var time = new Date();
			const y = time.getFullYear();
			const m = (time.getMonth() + 1 + "").padStart(2, "0");
			const d = (time.getDate() + "").padStart(2, "0");
			const hh = (time.getHours() + "").padStart(2, "0");
			const mm = (time.getMinutes() + "").padStart(2, "0");
			const ss = (time.getSeconds() + "").padStart(2, "0");

			var timer = `${y}-${m}-${d} ${hh}:${mm}`;
			var timing = `${y}-${m}-${d}`;
			this.timing = timing
			this.closeFormObj.fProcessdates = `${y}-${m}-${d}`;
			this.FirstLevel.fCheckdate1s = `${y}-${m}-${d}`; //一级审核日期
			this.secondLevel.fCheckdate2s = `${y}-${m}-${d}`; //一级审核日期
		},
		callback() {}
	}
}
